package com.qianya.system.manage.web.login.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.common.JWTUtil;
import com.qianya.model.pojo.login.vo.LoginUsersPermissionVO;
import com.qianya.system.manage.web.login.model.LoginUsers;
import com.qianya.system.manage.web.login.model.LoginUsersPermission;
import com.qianya.system.manage.web.login.service.ILoginUsersPermissionService;
import com.qianya.system.manage.web.login.mapper.LoginUsersPermissionMapper;
import com.qianya.system.manage.web.login.service.ILoginUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.qianya.model.common.ResultCode;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author lhl
* @since 2020-07-02
*/
@Service
public class LoginUsersPermissionServiceImpl extends ServiceImpl<LoginUsersPermissionMapper, LoginUsersPermission> implements ILoginUsersPermissionService {
    @Resource
    private LoginUsersPermissionMapper usersPermissionMapper;
    @Autowired
    private ILoginUsersService usersService;
        /**
        *查找 
        * @author lhl
        * @since 2020-07-02
        */
        @Override
        public List<LoginUsersPermissionVO> find(String uUsername,int type) {
            if ("".equals(uUsername)){
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                uUsername= JWTUtil.getUsername(request.getHeader("token"));
            }
            LoginUsers users=usersService.getOne(new QueryWrapper<LoginUsers>().eq("U_USERNAME",uUsername));
            if (users.getUType()==0){
                return usersPermissionMapper.usernameFindPermissionAll(type);
            }

           return usersPermissionMapper.usernameFindPermission(uUsername,type);
        }
        /**
        * 新增 
        * @author lhl
        * @since 2020-07-02
        */
        @Override
        public CommonResult create(LoginUsersPermission loginUsersPermission,HttpServletRequest request) {
            boolean b = save(loginUsersPermission);
            if (b){
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }
        /**
        * 修改 
        * @author lhl
        * @since 2020-07-02
        */
        @Override
        public CommonResult renewal(LoginUsersPermission loginUsersPermission,HttpServletRequest request) {
            boolean b = updateById(loginUsersPermission);
            if (b){
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }


}
